<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
</head>
<body>
<?php 

//having this separate script to avoid the memory problem with Zend Framework for view objects... jesus christ
$action = $_REQUEST['action'];
$language = $_REQUEST['language'];
include_once ("./db.php");
connect_db();
if ($action == "page")
{
	$page = $_REQUEST['page'];
	echo "<h1> $page </h1>";
	$company_name = get_post("company_name");
	echo  "company_name:" ; var_dump($company_name);
	$company_address = get_post("company_address");
	echo  "company_address:" ; var_dump($company_address);
	
	switch ($page)
	{
		case 'home':
		{
			$company_slogan = get_post("company_slogan");
			echo  "company_slogan:" ; var_dump($company_slogan);
			//get gallery of 4 category
			$gallery = get_gallery_by_title("project_thumbs", "gallery");
			echo "gallery"; var_dump($gallery);
		}	
		case 'about':
		{
			$company_about = get_post("company_about");
			echo  "company_about:" ; var_dump($company_about);
			$people = get_people();
			echo  "People:" ; var_dump($people);
			$office_pics = get_gallery_by_title("office_pics", "gallery");
			echo "office_pics: " ; var_dump($office_pics);
		}
		case 'project':
		{
			$category = $_REQUEST['category'];
			$projects = get_project($category);
			echo "Project list :"; dump($projects);
			die("done");
		}
		case 'recruitment':
		{
			$company_recruitment = get_post("company_recruitment");
			echo  "company_recruitment:" ; var_dump($company_recruitment);
			die("done");
		}
		case 'contact':
		{
			$company_recruitment = get_post("company_recruitment");
			echo  "company_recruitment:" ; var_dump($company_recruitment);
			die("done");
		}
	}
	
}
if ($action == "type")
{
	$type = $_REQUEST['type'];
	die($type);
}

function get_post($code)
{
	$sql = "SELECT * FROM `posts` WHERE `code`='$code'";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	return $row;
}

function get_gallery_by_title($title, $type)
{
	$sql = "SELECT * FROM `galleries` WHERE `title`='$title' AND `type`='$type'";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	$row['image_list'] = unserialize($row['images']);
	return $row;
}

function get_gallery_by_id($id)
{
	$sql = "SELECT * FROM `galleries` WHERE `id`=$id";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	$row['image_list'] = unserialize($row['images']);
	return $row;
}


function get_people()
{
	$sql = "SELECT * FROM `people` ORDER BY weight ASC";
	$result = mysql_query($sql);
	$ret = array();
	while ($row = mysql_fetch_assoc($result))
	{
		$row['avatar'] = "/public/archin/people/" . $row['id'] ."/avatar.jpg";
		$ret[] = $row;
	}
	return $ret;
}

function get_project($category)
{
	$sql = "SELECT * FROM `projects` WHERE `category`=$category ORDER BY weight ASC";
	$result = mysql_query($sql);
	$ret = array();
	while ($row = mysql_fetch_assoc($result))
	{
		//$row['avatar'] = "/public/archin/people/" . $row['id'] ."/avatar.jpg";
		$galleryId = $row['gallery'];
		if ($galleryId)
		{
			$gallery = 	get_gallery_by_id($galleryId);
			$row['image_list'] = $gallery['image_list'];
			
		}
		
		$ret[] = $row;
	}
	return $ret;
}

function dump($a)
{
	echo "<pre>";
    print_r($a);
    echo "</pre>";
}

?>
</body>
</html>
